package Engine.ParticelSystem;

import java.util.ArrayList;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;

import Engine.Funktions;

public class ParticleConfiguration {

	private Texture texture;
	private int Size;
	private int zindex;
	private int count;
	private float lifetime;
	private int YForce;
	private int XForce;
	private int StartAlpha = 255;
	private int EndAlpha = 0;
	
	private int StartSize = 16;
	private int EndSize = 8;
	
	private boolean UseAlphaBlending = true;
	private float RandomRotation;
	private java.util.List<Color> colors;

	public ParticleConfiguration(String name) {
		colors = new ArrayList<Color>();
		if (name.toLowerCase() == "fire") {
			setSize(8);
			setZindex(10);
			setParticleCount(500);
			colors.add(Color.orange);
			colors.add(Color.red);
			colors.add(Color.gray);
			setLifetime(02f);
			setYForce(-5);
			setRandomRotation(100);
		}
		
	
	}

	public Color color(float lifetime2) {

		/*
		 * float nortime2 = (float) (lifetime2 / lifetime * 0.1);// Die Zeit //
		 * normaliesirt. float ccd = 1f / (float) (colors.size() - 2); float
		 * nortime = nortime2 * (colors.size() - 2);// Die Zeit normaliesirt.
		 * int periode = (int) nortime;
		 * 
		 * System.out.println("Periode:" + periode + " nortime " + nortime +
		 * " ccd: "+nortime*ccd); float g = (colors.get(1 + periode).g * nortime
		 * * ccd) + (colors.get(0 + periode).g * (1 - nortime * ccd)); float b =
		 * (colors.get(1 + periode).b * nortime * ccd) + (colors.get(0 +
		 * periode).b * (1 - nortime * ccd)); float a = (colors.get(1 +
		 * periode).a * nortime * ccd) + (colors.get(0 + periode).a * (1 -
		 * nortime * ccd)); float r = (colors.get(1 + periode).r * nortime *
		 * ccd) + (colors.get(0 + periode).r * (1 - nortime * ccd));
		 */
		float r = 1;
		float g = 0;
		float b = 1;
		float a = 1;

		int color1index;
		int color2index;
		float timepercolor;
		int colorcount;
		float normalizedtime;
		float relativetime;
		colorcount = colors.size();
		normalizedtime = lifetime2 / (lifetime * 010f);
		timepercolor = 1 / ((float) colors.size() - 1f);
		color1index = (int)(normalizedtime / timepercolor);
		color2index = color1index + 1;
		while (color2index >= colors.size())
			color2index -= 1;

		float tmp = normalizedtime;

		while (tmp >= timepercolor)
			tmp -= timepercolor;
		relativetime = tmp / timepercolor;

		// System.out.println(relativetime);
		r = ((float) colors.get(color2index).r * relativetime)
				+ ((float) colors.get(color1index).r * (1 - relativetime));

		g = ((float) colors.get(color2index).g * relativetime)
				+ ((float) colors.get(color1index).g * (1 - relativetime));

		b = ((float) colors.get(color2index).b * relativetime)
				+ ((float) colors.get(color1index).b * (1 - relativetime));

		a = (getEndAlpha() * normalizedtime)
				+ (getStartAlpha() * (1 - normalizedtime));

		// System.out.println(1 - relativetime);

		return new Color(r, g, b, a);
	}

	public void Init() {
		setTexture(Funktions.loadTexture("Engine/Particle.png", GL11.GL_LINEAR));
	}

	public Texture getTexture() {
		return texture;
	}

	public void setTexture(Texture texture) {
		this.texture = texture;
	}

	public float getSize(float lifetime2) {
		float a;
		float normalizedtime = lifetime2 / (lifetime * 010f);
		a = (getEndSize() * normalizedtime)
				+ (getStartSize() * (1 - normalizedtime));
		
		return a;
	}

	public void setSize(int size) {
		Size = size;
	}

	public int getZindex() {
		return zindex;
	}

	public void setZindex(int zindex) {
		this.zindex = zindex;
	}

	public int getParticleCount() {
		return count;
	}

	public void setParticleCount(int count) {
		this.count = count;
	}

	public float getLifetime() {
		return lifetime;
	}

	public void setLifetime(float lifetime) {
		this.lifetime = lifetime;
	}

	public int getXForce() {
		return XForce;
	}

	public void setXForce(int xForce) {
		XForce = xForce;
	}

	public int getYForce() {
		return YForce;
	}

	public void setYForce(int yForce) {
		YForce = yForce;
	}

	public float getRandomRotation() {
		return RandomRotation;
	}

	public void setRandomRotation(float randomRotation) {
		RandomRotation = randomRotation;
	}

	public boolean isUseAlphaBlending() {
		return UseAlphaBlending;
	}

	public void setUseAlphaBlending(boolean useAlphaBlending) {
		UseAlphaBlending = useAlphaBlending;
	}

	public float getEndAlpha() {
		return EndAlpha / 255f;
	}

	public void setEndAlpha(int endAlpha) {
		EndAlpha = endAlpha;
	}

	public float getStartAlpha() {
		return StartAlpha / 255f;
	}

	public void setStartAlpha(int startAlpha) {
		StartAlpha = startAlpha;
	}

	public int getStartSize() {
		return StartSize;
	}

	public void setStartSize(int startSize) {
		StartSize = startSize;
	}

	public int getEndSize() {
		return EndSize;
	}

	public void setEndSize(int endSize) {
		EndSize = endSize;
	}

}
